package com.sgb.kjwl.viewmodel.self;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.model.entity.TypeEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.SupplierAuthEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthInfoActivity;
import com.swgk.core.base.BaseViewModel;

import java.util.Arrays;
import java.util.List;

public class AuthInfoModel extends BaseViewModel {
    public ObservableField<SupplierAuthEntity> authEntity = new ObservableField<>();
    public ObservableField<UserMsgEntity> userMsgEntity = new ObservableField<>(new UserMsgEntity());
    private AuthInfoActivity activity;
    private SelfRepertory repertory;

    public AuthInfoModel(AuthInfoActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getServiceTypeModelEx(UserMsgEntity data) {
    /*    APIIMManager.getInstance().getIMServiceAPI().getServiceType().enqueue(new ICallBack<BaseEntity<List<TypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TypeEntity>> result) {
                if (data.getEnterprise() != null) {
                    String serviceType = data.getEnterprise().getServiceType();
                    String serviceTypeStr = getServiceTypeStr(result.getData(), serviceType);
                    data.getEnterprise().setServiceTypeStr(serviceTypeStr);
                }
                activity.showDetailView(data);
                authEntity.set(data.getEnterprise());
                userMsgEntity.set(data);
            }
        });*/
    }
    /**
     * 解析并匹配组装经营类型
     * @param data 次级列表
     * @param serviceType 已选的经营类型
     * @return
     */
    private String getServiceTypeStr(List<TypeEntity> data, String serviceType){
        if (TextUtils.isEmpty(serviceType)){
            return "";
        }
        String[] split = serviceType.split(",");
        if (split.length == 0){
            return "";
        }
        List<String> strings = Arrays.asList(split);
        if (strings == null){
            return "";
        }
        StringBuilder sbName = new StringBuilder();
        for (int i = 0; i < data.size(); i++) {
            TypeEntity typeEntity = data.get(i);
            if (strings.contains(typeEntity.getCode())){
                sbName.append(typeEntity.getName()).append(",");
            }
        }
        return sbName.substring(0, sbName.length() - 1);
    }

}
